if exists (SELECT * from dbo.sysobjects WHERE id = object_id(N'[dbo].[vw_purchases_orders_distributions_amounts]') AND OBJECTPROPERTY(id, N'IsView') = 1)
drop view [dbo].[vw_purchases_orders_distributions_amounts]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

/***********************************************************************
* vw Name: dbo.vw_purchases_orders_distributions_amounts
* Created: 20101118, RGM
* Purpose: listado de las po y el monto de lo etiquetado y lo no etiquetado
*	general
*
* Modified: yyyymmdd, user, data
*
* HistoryDate.
* yyyymmdd User, data
* 
* Note: nothing
* 
***********************************************************************/
CREATE VIEW dbo.vw_purchases_orders_distributions_amounts AS
SELECT pov.purchase_order_version_id, vpv.month_period_version_id, 
		pov.capacity, SUM(ISNULL(pd.quantity, 0)) qty_labeled, 
		(pov.capacity - SUM(ISNULL(pd.quantity, 0))) qty_not_labeled
	FROM purchases_orders_versions pov
		INNER JOIN vw_periods_versions vpv
			ON pov.biweek_period_version_id = vpv.biweek_period_version_id
		LEFT OUTER JOIN vw_plants_distributions pd
			ON pov.purchase_order_version_id = pd.purchase_order_version_id
	GROUP BY pov.purchase_order_version_id, vpv.month_period_version_id, 
		pov.capacity

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

